package com.nifostasky.config;


import com.netflix.hystrix.HystrixCommand;
import com.nifostasky.entity.Student;
import org.springframework.web.client.RestTemplate;

/**
 * 自定义熔断器
 */
public class CustomHystrixCommand extends HystrixCommand<Student> {

    private RestTemplate restTemplate;

    private Integer id;

    //格式: http://[服务对应服务名]/student/queryUserById/
    private static final String URLEureka = "http://springcloud-controller/student/queryUserById/";


    public CustomHystrixCommand(Setter setter,RestTemplate restTemplate,Integer id){
        super(setter);
        this.restTemplate = restTemplate;
        this.id = id;
    }

    @Override
    protected Student run() throws Exception {
        System.out.println( URLEureka + id +";;;;;;;;;;;;;;;;;;;;");
        System.out.println(restTemplate.getForObject(URLEureka + id , Student.class).toString());
        return restTemplate.getForObject(URLEureka + id , Student.class);
    }

    @Override
    protected Student getFallback() {
        System.out.println("========================自定义熔断器==================================");
        //获取异常信息
        Throwable throwable = super.getExecutionException();
        System.out.println("异常信息: \n"+ throwable.getMessage());
        return new Student();
    }
}
